package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.QueryVo;
import com.qf.pojo.Speaker;
import com.qf.service.SpeakerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 张鑫
 * @date 2020/10/20 10:45
 */
@Controller
@RequestMapping("speaker")
public class SpeakerController {

    @Autowired
    private SpeakerService speakerService;

    // 展示讲师信息
    @RequestMapping("showSpeakerList")
    public String showSpeakerList(Model model,
                                  @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<Speaker> speakerList = speakerService.findAll();
        PageInfo pageInfo = new PageInfo(speakerList);
        model.addAttribute("page", pageInfo);
        return "/behind/speakerList.jsp";
    }

    // 跳转添加讲师信息
    @RequestMapping("addSpeaker")
    public String addSpeaker() {

        return "/behind/addSpeaker.jsp";
    }

    // 添加或修改讲师信息
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Speaker speaker, Integer id) {
        if (id == null) {
            speakerService.addSpeaker(speaker);
        } else {
            speakerService.updateSpeaker(speaker);
        }
        return "redirect:/speaker/showSpeakerList";
    }

    // 编辑讲师信息
    @RequestMapping("edit")
    public String edit(Integer id, Model model) {
        Speaker speaker = speakerService.findById(id);
        model.addAttribute("speaker", speaker);

        return "/behind/addSpeaker.jsp";
    }

    // 删除讲师信息
    @ResponseBody
    @RequestMapping("speakerDel")
    public String speakerDel(Integer id) {
        speakerService.delSpeaker(id);
        return "success";
    }
}
